package com.ui.home.presenter;

import com.base.BaseRefreshPresenter;
import com.bean.BaseBean;
import com.bean.SearchHotKeyBean;
import com.bean.SearchListBean;
import com.net.ApiService;
import com.net.RetrofitServiceManager;
import com.net.RxResponseObserver;
import com.net.RxSchedulers;

import java.util.ArrayList;
import java.util.List;

public class SearchActivityPresenter extends BaseRefreshPresenter<SearchActivityContract.View> implements SearchActivityContract.Presenter {
    @Override
    public void getHotKey() {

        RetrofitServiceManager.getInstance().create(ApiService.class).getHotKeyList()
                .compose(RxSchedulers.<BaseBean<List<SearchHotKeyBean>>>normalSchedulers())
                .compose(mView.<BaseBean<List<SearchHotKeyBean>>>bindToLife())
                .subscribe(new RxResponseObserver<BaseBean<List<SearchHotKeyBean>>>() {
                    @Override
                    public void onSuccess(BaseBean<List<SearchHotKeyBean>> listBaseBean) {
                        List<SearchHotKeyBean> data = listBaseBean.getData();
                        List<String> keyList = new ArrayList<>();
                        for (SearchHotKeyBean key : data) {
                            keyList.add(key.getName());
                        }
                        mView.onGetHotKey(keyList);
                    }
                });
    }

    @Override
    public void getSearchList(final int page, String keyWord) {
        RetrofitServiceManager.getInstance().create(ApiService.class).getSearchList(page,keyWord)
                .compose(RxSchedulers.<BaseBean<SearchListBean>>normalSchedulers())
                .compose(mView.<BaseBean<SearchListBean>>bindToLife())
                .subscribe(new RxResponseObserver<BaseBean<SearchListBean>>() {
                    @Override
                    public void onSuccess(BaseBean<SearchListBean> searchListBeanBaseBean) {
                        if (page == 0) {
                            mView.onGetSearchList(searchListBeanBaseBean.getData().getDatas());
                        }else {
                            mView.onLoadMoreSearch(searchListBeanBaseBean.getData().getDatas());
                        }
                    }
                });
    }
}
